#import "AppDelegate.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import <UserNotifications/UserNotifications.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <JJException.h>
#import "CTAFManager.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
#import <AdSupport/AdSupport.h>
#import "CTService.h"
#import <CoreMotion/CoreMotion.h>
#import "SSKeychain.h"
#import <FBSDKCoreKit/FBSDKCoreKit-Swift.h>
#import <FBSDKCoreKit/FBSDKApplicationDelegate.h>
@interface AppDelegate () <JJExceptionHandle,UIApplicationDelegate, UNUserNotificationCenterDelegate>
@property (strong, nonatomic) CMMotionManager *motionManager;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#ifdef PRODUCTION
    [JJException configExceptionCategory:JJExceptionGuardAll];
    [JJException startGuardException];
    [JJException registerExceptionHandle:self];
#endif
    [CTService configService];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [CTControllerManager ct_configRootVC];
    [self.window makeKeyAndVisible];
    [CTAFManager shared];
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    [[CTLocationManager sharedManager] startMonitoringSignificantLocationChanges];
    [CTUserManager shareManager].var_sessionId = kGetCurentTime;
    if ([CTUserManager appIsFirstLaunched]) {
        CTUserModel *localModel = [CTUserManager shareManager].userModel;
        localModel.installTime = kGetCurentTime;
        [[CTUserManager shareManager] updateUserInfo:localModel];
    }
    [CTUploadManager ct_uploadType:CTUploadTypeLaunch];
    NSDictionary *data = @{
        @"navigationRoute" : @"CTLaunchImageVC",
        @"pageIdentity" : @"ng_acv_001_00",
        @"elementUniqueIdentifier" : @"ng_acv_001_00",
        @"referenceIdentification" : @"",
    };
    [CTUploadManager ct_uploadType:CTUploadTypePage data:data];
    [self getMotionInfo];
    [self configIQKeyBoard];
    [NSThread sleepForTimeInterval:3.0];
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60.0];
    if (@available(iOS 14.0, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
        }];
    }
    [[AppsFlyerLib shared] start];
}
- (void)getMotionInfo {
    self.motionManager = [[CMMotionManager alloc] init];
    [self.motionManager startGyroUpdates];
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *x = [NSString stringWithFormat:@"%.8f",weakSelf.motionManager.gyroData.rotationRate.x];
        NSString *y  = [NSString stringWithFormat:@"%.8f",weakSelf.motionManager.gyroData.rotationRate.y];
        NSString *z  = [NSString stringWithFormat:@"%.8f",weakSelf.motionManager.gyroData.rotationRate.z];
        NSLog(@"x %@,y %@,z %@: ", x, y, z);
        NSDictionary *dictInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                  x, @"angularMeasurementX",
                                  y, @"angularMeasurementY",
                                  z, @"angularMeasurementZ",
                                  nil];
        NSLog(@"launchVC_dictTlyInfo %@",dictInfo);
        [CTUploadManager ct_uploadType:CTUploadTypeMotion data:dictInfo];
        [weakSelf.motionManager stopGyroUpdates];
    });
}
- (void)configIQKeyBoard {
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}
- (void)handleCrashException:(nonnull NSString *)exceptionMessage extraInfo:(nullable NSDictionary *)info {
}
@end
